package com.yilin.platform.controller.user;


import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.application.UserUtil;
import com.yilin.platform.base_core.commons.annotation.SkipLoginVerify;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.entity.system.Jurisdiction;
import com.yilin.platform.entity.user.Account;
import com.yilin.platform.entity.user.Authority;
import com.yilin.platform.service.impl.system.JurisdictionService;
import com.yilin.platform.service.impl.user.AccountService;
import com.yilin.platform.service.impl.user.AuthorityService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user/authority/")
public class AuthorityController extends BaseController {
    @Resource
    private AuthorityService authorityService;
    @Resource
    private AccountService accountService;
    @Resource
    private JurisdictionService jurisdictionService;

    @PostMapping("updateAuthority")
    @SkipLoginVerify
    public ReturnData<Object> updateAuthority(String accountId, String[] jurisdictionIds) {
        ParamUtil.isNull(accountId, "请传入ID参数");
        Account account = accountService.getAccountById(accountId);
        ParamUtil.isNull(account, "获取用户失败");
        List<Jurisdiction> jurisdictionList =jurisdictionService.getJurisdictionListByIds(jurisdictionIds);
        authorityService.saveAuthority(account, jurisdictionList);
        UserUtil.removeLoginUser(account.getToken());
        return jsonView(true, "保存成功");
    }

    @PostMapping("getAuthorityList")
    @SkipLoginVerify
    public ReturnData<Object> getAuthorityList(String accountId) {
        ParamUtil.isNull(accountId, "请传入ID参数");
        List<Authority> authorityList = authorityService.getAuthorityListByyAccountId(accountId);
        return jsonView(true, "保存成功", authorityList);
    }


}
